/*-------------------------------------------------------------------------------
* file:     app_init.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2022-08-15
* Version:  V1.00
* Author:   JMH
*-------------------------------------------------------------------------------*/

/* include ----------------------------------------------------------------*/
#include "app_init.h"
#include "app_parameter.h"
/* externs function--------------------------------------------------------*/
extern void board_init(void);

//iic有关初始化标志
volatile uint8_t global_tp_init_timer_flag;
//iic有关初始化tick
volatile uint16_t global_tp_init_timer_tick;
//硬件版本选择
volatile uint8_t global_dvt_or_evt_choose;
//初始化完成标志
volatile bool global_soc_init_succes_flag;

volatile uint8_t global_onewirebus_phy;

/* Private function--------------------------------------------------------*/
/**
 * @brief 关闭时钟
 */
void app_power_saving_clk_disable(void)
{
    csi_clk_disable((uint32_t *)I2C0);
    csi_clk_disable((uint32_t *)SIO0);
    csi_clk_disable((uint32_t *)SPI0);
    csi_clk_disable((uint32_t *)USART0);
    csi_clk_disable((uint32_t *)UART0);
    csi_clk_disable((uint32_t *)UART1);
    csi_clk_disable((uint32_t *)UART2);
    csi_clk_disable((uint32_t *)ETCB);
    // csi_clk_disable((uint32_t *)TKEY);
    csi_clk_disable((uint32_t *)ADC0);
    // csi_clk_disable((uint32_t *)IFC);
    // csi_clk_disable((uint32_t *)LCD);
    // csi_clk_disable((uint32_t *)CMP1);
    // csi_clk_disable((uint32_t *)CMP0);
    // csi_clk_disable((uint32_t *)LED);
    csi_clk_disable((uint32_t *)BT1);
    csi_clk_disable((uint32_t *)BT0);
    csi_clk_disable((uint32_t *)RTC);
    csi_clk_disable((uint32_t *)CNTA);
    csi_clk_disable((uint32_t *)LPT);
    csi_clk_disable((uint32_t *)WWDT);
    csi_clk_disable((uint32_t *)GPTB1);
    csi_clk_disable((uint32_t *)GPTB0);
    csi_clk_disable((uint32_t *)GPTA1);
    csi_clk_disable((uint32_t *)GPTA0);
    csi_clk_disable((uint32_t *)EPT0);
}

/**
 * @brief 设置唤醒中断
 * 
 */
void app_set_wakeup_irq(void)
{
#if POWER_SAVING_ENABLE
    csi_vic_set_wakeup_irq(SYSCON_IRQ_NUM);//SYSCON
    csi_vic_set_wakeup_irq(IFC_IRQ_NUM);//IFC
    csi_vic_set_wakeup_irq(SPI_IRQ_NUM);//SPI0  
    csi_vic_set_wakeup_irq(I2C_IRQ_NUM);//I2C

    csi_vic_set_wakeup_irq(DMA_IRQ_NUM);//dma 
    csi_vic_set_wakeup_irq(UART0_IRQ_NUM);
    csi_vic_set_wakeup_irq(UART1_IRQ_NUM);//log out
    csi_vic_set_wakeup_irq(UART2_IRQ_NUM);//one wire uart
    csi_vic_set_wakeup_irq(BT0_IRQ_NUM);//timer
    //   csi_vic_set_wakeup_irq(BT1_IRQ_NUM);
    csi_vic_set_wakeup_irq(CORET_IRQ_NUM);//Coret
#endif
}

/**
 * @brief 清理中断
 * 
 */
void app_clear_wakeup_irq(void)
{
#if POWER_SAVING_ENABLE
    csi_vic_clear_wakeup_irq(SYSCON_IRQ_NUM);
    csi_vic_clear_wakeup_irq(IFC_IRQ_NUM);
    csi_vic_clear_wakeup_irq(SPI_IRQ_NUM);
    csi_vic_clear_wakeup_irq(I2C_IRQ_NUM);

    csi_vic_clear_wakeup_irq(DMA_IRQ_NUM);
    csi_vic_clear_wakeup_irq(UART0_IRQ_NUM);
    csi_vic_clear_wakeup_irq(UART1_IRQ_NUM);
    csi_vic_clear_wakeup_irq(UART2_IRQ_NUM);
    csi_vic_clear_wakeup_irq(BT0_IRQ_NUM);
    //   csi_vic_clear_wakeup_irq(BT1_IRQ_NUM);
    csi_vic_clear_wakeup_irq(CORET_IRQ_NUM);
#endif
}

/**
 * @brief app初始化
 */
void app_init(void)
{
    global_onewirebus_phy = 60;
    global_tp_init_timer_flag = 0x00;
    global_tp_init_timer_tick = 0x00;
    global_dvt_or_evt_choose = 0x00;
    global_soc_init_succes_flag = false;

    app_iwt_clr();
    //定时器初始化
    app_timer_init();
	//产测有关初始化
    app_factory_init();
    //读取硬件版本
    app_hardware_version_read();
    //UART初始化
    app_uart_init();
    //按键扫描初始化
    app_key_init(true);
    //iic初始化
//    app_iic_init();
    //spi初始化
    app_spi_master_init();
    //单总线部分初始化
    one_wire_bus_app_init();
    //低功耗部分初始化
    app_lowpower_init();
    //升级部分初始化
    app_updata_init(); 

    //产测有关初始化
//    app_factory_init();

    //flash_log初始化
//    app_flash_log_init();
	log_init();
	
    //休眠定时器初始化
    app_sleep_timer_init();
    #if FLASH_LOG_PRINTF_FLAG
        app_flash_log_printf(":powerON",NULL,0);
    #endif
    //开启唤醒中断
    app_set_wakeup_irq();
    global_soc_init_succes_flag = true;
    app_iwt_clr();
	#if APP_DEBUG_POWER_ON_LOG_ENABLE
        printf("Power on:%d\r\n",SYSCON->RSR);
    #endif
	
	rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_PWR_OFF,0x01,0x01,SYSCON->RSR);
	rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_PWR_OFF,0x02,0x01,SYSCON->RSR>>8);
	


    csi_pin_set_mux(IIC_PING_SCL,PA112_OUTPUT);
    csi_pin_set_mux(IIC_PIN_SDA,PA112_OUTPUT);
	
	csi_pin_pull_mode(IIC_PING_SCL,GPIO_PULLDOWN);
	csi_pin_pull_mode(IIC_PIN_SDA,GPIO_PULLDOWN);
	csi_pin_set_low(IIC_PING_SCL);
	csi_pin_set_low(IIC_PIN_SDA);
	
	csi_pin_set_mux(PA011,PA011_GPD);                         //PA02 输入
	csi_pin_pull_mode(PA011, GPIO_PULLUP); 
//	csi_gpio_port_set_high(GPIOA0,(0x01ul << 2));
	
//	csi_gpio_port_dir(GPIOA0, (0x01ul << 15), GPIO_DIR_INPUT);       // GPIOA1.15 输入
//    csi_gpio_port_pull_mode(GPIOA0, (0x01ul << 15), GPIO_PULLUP);    // 上拉
}

/**
 * @brief 初始化计时时基
 * 
 */
void app_iic_init_timer_tick_0_1ms_handler(void)
{
//    if(global_tp_init_timer_tick <= (3000))
    {
        global_tp_init_timer_tick ++;
    }

}

/**
 * @brief 看门狗清理
 * 
 */
void app_iwt_clr(void)
{
    // csi_iwdt_feed();
    SYSCON -> IWDCNT = (SYSCON -> IWDCNT & (~IWDT_CLR_MSK)) | IWDT_CLR << IWDT_CLR_POS;
}

extern volatile bool tp_init_flag;
extern volatile bool global_gsenser_init_flag;
/**
 * @brief  需要上电后延迟初始化的部分
 * 
 */
void app_delay_init(void)
{
    
    //触摸板及gsensor可能上电后一会才能相应指令，要延迟初始化
	static uint8_t init_flag=0;
	if(global_tp_init_timer_tick>TP_SCAN_TIMER_N && init_flag==0)
	{
		csi_pin_set_mux(PA011,PA011_OUTPUT);                         //PA02 输入
		csi_pin_pull_mode(PA011, GPIO_PULLDOWN);
		csi_pin_set_low(PA011);
		app_iic_reset();
		init_flag=1;
	}
	 if(global_tp_init_timer_tick > Gsensor_SCAN_TIMER_N&&init_flag==1)
	{
		
		app_Gsensor_init();  
		if(!global_gsenser_init_flag)
		{
//			csi_pin_set_mux(PA011,PA011_GPD);                         //PA02 输入
//			csi_pin_pull_mode(PA011, GPIO_PULLUP);
			global_tp_init_timer_tick=0;
			kb_lock_state=0;
		}    
		init_flag==0;
	}
	
	
	
	
//		uint8_t data_buffer[8] = {0};
//		
//		one_wire_bus_app_send_handleshake(0, (uint8_t *)&data_buffer[0],0);
//		delay_ums(200);
//		one_wire_bus_app_send_keyboard(0,(uint8_t *)&data_buffer[0],3);
		
		
}

/**
 * @brief 开机读取硬件版本
 * 
 */
uint8_t app_hardware_version_read(void)
{
    uint8_t herdware_ver = 0x00;
    csi_pin_set_mux(HARDWARE_IO,PA19_INPUT);
	csi_pin_pull_mode(HARDWARE_IO,GPIO_PULLNONE);
	
	udelay(10);
    if(csi_pin_read(HARDWARE_IO))
    {
        herdware_ver = 0x02;
        global_dvt_or_evt_choose = 0;
    }else
    {
        global_dvt_or_evt_choose = 1;
        herdware_ver = 0x03;
//		device_version[3]='1';
    }
    #if APP_DEBUG_POWER_ON_LOG_ENABLE   
        printf("global_dvt_or_evt_choose = %d\n",global_dvt_or_evt_choose);
    #endif
    #if (APP_LOG_ALL_ENABLE == 0)
//        csi_pin_set_mux(PB02,PB02_OUTPUT);
//        csi_pin_set_high(PB02);
        // csi_pin_pull_mode(PB02,GPIO_PULLUP);
    #endif
    return herdware_ver;
}

/**
 * @brief 硬件io休眠函数
 * 
 * @param on false 休眠
 */
void app_hardware_ver_lowpower_io_set(bool on)
{
    if(on)
    {
//        csi_pin_set_mux(PB02,PB02_OUTPUT);
//        csi_pin_set_high(PB02);
    }else
    {
//        if(global_dvt_or_evt_choose == 0x00)
//        {
//            //EVT
//            csi_pin_set_mux(PB02,PB02_GPD);
//            csi_pin_pull_mode(PB02,GPIO_PULLDOWN);
//            csi_pin_set_mux(HARDWARE_IO,PA19_GPD);
//            csi_pin_pull_mode(HARDWARE_IO,GPIO_PULLUP);
//        }else
        {
            //DVT
//            csi_pin_set_mux(PB02,PB02_GPD);
            csi_pin_set_mux(HARDWARE_IO,PA111_GPD);
            csi_pin_pull_mode(HARDWARE_IO,GPIO_PULLDOWN);
        }
        
    }
}

